package com.snbc.Exception;

import java.math.BigDecimal;

/**
 * @Classname CheckingAccount
 * @Description 支票账户，具备透支额度，继承Account
 * 属性：overdraftProtection 透支额度
 * @Date 2019/9/15 22:49
 * @Created by Acer
 */
public class CheckingAccount extends Account {

    private BigDecimal overdraftProtection;

    public CheckingAccount(BigDecimal account1, BigDecimal overdraftProtection) {
        super(account1);
        this.overdraftProtection = overdraftProtection;
        System.out.println("当前账户余额：" + account1 + "\n" + "透支额度为：" + overdraftProtection);
    }


    @Override
    public void withdraw(BigDecimal money) throws OverdraftException {
        if (money.compareTo(account.add(overdraftProtection)) == 1) {
            System.out.println();
            throw new OverdraftException("余额及提现额度不足：" + (account.add(overdraftProtection)).subtract(money));
        } else {
            if (money.compareTo(account) == -1) {
                account = account.subtract(money);
                System.out.println("取款金额为：" + money);
                System.out.println("账户余额为：" + account);
            } else {
                overdraftProtection = (overdraftProtection).subtract(money.subtract(account)); // 余额不够，使用透支额度

                System.out.println("取款成功！" + "取款金额为：" + money + "\n" + "余额为：0元" + "\n" + "信用额度为：" + overdraftProtection);
            }

        }
    }
}
